/*
 *  Copyright 2009 Rohan Ranade.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.netbeans.modules.cpextender.ui.customizer;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Rohan Ranade
 */
public class CPExtensionPanel extends javax.swing.JPanel {

    public CPExtensionPanel(boolean isEnabled, String filePath) {
        initComponents();
        this.checkEnableClasspath.setSelected(isEnabled);
        if (filePath != null) {
            this.textFilePath.setText(filePath);
        }
        checkEnableClasspathActionPerformed(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        checkEnableClasspath = new javax.swing.JCheckBox();
        labelFile = new javax.swing.JLabel();
        textFilePath = new javax.swing.JTextField();
        buttonBrowse = new javax.swing.JButton();

        checkEnableClasspath.setText(org.openide.util.NbBundle.getMessage(CPExtensionPanel.class, "CPExtensionPanel.checkEnableClasspath.text")); // NOI18N
        checkEnableClasspath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkEnableClasspathActionPerformed(evt);
            }
        });

        labelFile.setText(org.openide.util.NbBundle.getMessage(CPExtensionPanel.class, "CPExtensionPanel.labelFile.text")); // NOI18N
        labelFile.setEnabled(false);

        textFilePath.setEditable(false);
        textFilePath.setText(org.openide.util.NbBundle.getMessage(CPExtensionPanel.class, "CPExtensionPanel.textFilePath.text")); // NOI18N
        textFilePath.setEnabled(false);

        buttonBrowse.setText(org.openide.util.NbBundle.getMessage(CPExtensionPanel.class, "CPExtensionPanel.buttonBrowse.text")); // NOI18N
        buttonBrowse.setEnabled(false);
        buttonBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowseActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(21, 21, 21)
                        .add(labelFile)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(textFilePath, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(buttonBrowse))
                    .add(checkEnableClasspath))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(checkEnableClasspath)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(labelFile)
                    .add(textFilePath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(buttonBrowse))
                .addContainerGap(242, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void checkEnableClasspathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkEnableClasspathActionPerformed
        boolean state = checkEnableClasspath.isSelected();
        labelFile.setEnabled(state);
        textFilePath.setEnabled(state);
        buttonBrowse.setEnabled(state);
    }//GEN-LAST:event_checkEnableClasspathActionPerformed

    private void buttonBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowseActionPerformed
        // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().endsWith("txt");
            }

            @Override
            public String getDescription() {
                return "Class Path Extension files (txt)";
            }
        });
        int retVal = chooser.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == retVal) {
            textFilePath.setText(chooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_buttonBrowseActionPerformed

    public boolean isExtensionEnabled() {
        return checkEnableClasspath.isSelected();
    }

    public String getExtensionFilePath() {
        return textFilePath.getText().trim();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBrowse;
    private javax.swing.JCheckBox checkEnableClasspath;
    private javax.swing.JLabel labelFile;
    private javax.swing.JTextField textFilePath;
    // End of variables declaration//GEN-END:variables

}
